package no.ntnu.fp.test;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.Date;

/**
 * The <code>Person</code> class stores information about a single well.
 * 
 */
public class Well {
	
	private String name;
	private String graph;
	
	private Date dateOfBirth;
	
	/**
	 * This member variable holds a unique identifier for this object.
	 */
	private long id;
	
	/**
	 * This member variable provides functionality for notifying of changes to
	 * the <code>Group</code> class.
	 */
	private PropertyChangeSupport propChangeSupp;
	
	/**
	 * Constant used when calling 
	 * {@link java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)}
	 * on {@linkplain #addPropertyChangeListener(java.beans.PropertyChangeListener) registered
	 * <code>PropertyChangeListener<code> objecs} when the person's name is changed.
	 * 
	 * @see #setName(String) the setName(String) method
	 */
	public final static String NAME_PROPERTY_NAME = "name";

	public final static String WELL_PROPERTY_NAME = "well";
	
	public final static String DATEOFBIRTH_PROPERTY_NAME = "dateOfBirth";
	
	/**
	 * Default constructor. Must be called to initialise the object's member variables.
	 * The constructor sets the name and email of this person to empty
	 * {@link java.lang.String}, while the date of birth is given today's date. The 
	 * {@linkplain #getId() id field} is set to current time when the object is created.
	 */
	public Well() {
		name = "";
		graph = "";
		dateOfBirth = new Date();
		id = System.currentTimeMillis();
		propChangeSupp = new PropertyChangeSupport(this);
	}
	
	/**
	 * Constructs a new <code>Person</code> object with specified name, email, and date
	 * of birth.
	 * 
	 * @param name The name of the person.
	 * @param email The person's e-mail address
	 * @param dateOfBirth The person's date of birth.
	 */
	public Well(String name, String email, Date dateOfBirth) {
		this();
		this.name = name;
		this.graph = graph;
		this.dateOfBirth = dateOfBirth;
	}
	

	public void setName(String name) {
		String oldName = this.name;
		this.name = name;
		PropertyChangeEvent event = new PropertyChangeEvent(this, NAME_PROPERTY_NAME, oldName, name);
		propChangeSupp.firePropertyChange(event);
	}
	
	public void setGraph(String graph) {
		String oldGraph = this.graph;
		this.graph = graph;
		PropertyChangeEvent event = new PropertyChangeEvent(this, WELL_PROPERTY_NAME, oldGraph, this.graph);
		propChangeSupp.firePropertyChange(event);
	}
	
	
	public void setDateOfBirth(Date dateOfBirth) {
		Date oldDateOfBirth = this.dateOfBirth;
		this.dateOfBirth = dateOfBirth;
		PropertyChangeEvent event = new PropertyChangeEvent(this, DATEOFBIRTH_PROPERTY_NAME, oldDateOfBirth, this.dateOfBirth);
		propChangeSupp.firePropertyChange(event);
	}
	
	/**
	 * Returns the well's name.
	 * 
	 * @return The well's name.
	 */
	public String getName() {
		return name;
	}

	
	public String getGraph() {
		return graph;
	}
	
	public Date getDateOfBirth() {
		return dateOfBirth;
	}
	
	
	public long getId() {
		return id;
	}

	/**
	 * Add a {@link java.beans.PropertyChangeListener} to the listener list.
	 * 
	 * @param listener The {@link java.beans.PropertyChangeListener} to be added.
	 */
	public void addPropertyChangeListener(PropertyChangeListener listener) {
		propChangeSupp.addPropertyChangeListener(listener);
	}
	
	/**
	 * Remove a {@link java.beans.PropertyChangeListener} from the listener list.
	 * 
	 * @param listener The {@link java.beans.PropertyChangeListener} to be removed.
	 */
	public void removePropertyChangeListener(PropertyChangeListener listener) {
		propChangeSupp.removePropertyChangeListener(listener);
	}
	
	/**
	 * {@inheritDoc}
	 */
	public boolean equals(Object obj) {
		if (super.equals(obj))
			return true;
		
		if (obj.getClass() != this.getClass())
			return false;
		
		Well aWell = (Well)obj;
		
		if (aWell.getName().compareTo(getName()) != 0) 
			return false;
		if (aWell.getGraph().compareTo(getGraph()) != 0)
			return false;
		if (aWell.getDateOfBirth().compareTo(getDateOfBirth()) != 0)
			return false;
		
		return true;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public String toString() {
		String s = "Name: " + getName() + "; ";
		s += "Graph: " + getGraph() + "; ";
		s += "Date of birth: " + getDateOfBirth().toString();
		return s;
	}
}
